/**
 * @Description: commit信息提交
 * @author Beon
 * @date 2021/1/14
*/
import {Command} from '../types/command';
import {OraLoading} from '../decorators/oraLoading';
import {ClassTool} from '../decorators/classTool';
import {Diff} from '../git/diff';
import {Add} from '../git/add';
import {Status} from '../git/status';
import {backJiraData, JiraInquirer} from '../inquirer/jiraInquirer';
import {backCommitData, CommitBranchInquirer} from '../inquirer/commitBranchInquirer';
import * as wrap from 'word-wrap';
import {Commit} from '../git/commit';
import {MessageDeal} from '../utils/messageDeal';
import {BranchOrigin} from '../git/branchOrigin';
import {OtherInquirer} from '../inquirer/otherInquirer';

export class LzCommit implements Command {

    @OraLoading.beProxy
    status: OraLoading;
    static COMMITDATA: backCommitData
    static JIRADATA: backJiraData
    static ADDFILES: string[]

    constructor() {
        this.status.value = '开始提交检查';
    }

    /**
     * 指令运行函数
     * @param {[String]} arg 参数
     * @returns {Promise<void>}
     */
    @ClassTool.saveThis
    async done(arg?: [string]): Promise<void> {
        await LzCommit.checkCommit(arg ? arg[0] : undefined);

        this.status.value = '提交准备';

        await LzCommit.prompter();

        const DEALDATA = LzCommit.dealCommitData();

        this.status.value = '提交';

        await Commit.dealCommit(DEALDATA, arg ? arg[0] : undefined);

        this.status.value = '';
    }

    /**
     * 提交前检查
     * @returns {Promise<void>}
     */
    static async checkCommit(repoPath?: string): Promise<void> {
        await Diff.isCleanError(repoPath); // 判断是否有修改

        this.ADDFILES = await Status.getAddFiles(repoPath); // 判断是否有修改

        await Add.addFile(await OtherInquirer.checkboxInquirerFn(this.ADDFILES), repoPath); // 自自动化add文件
    }

    /**
     * 收集commit提问数据
     * @returns {Promise<void>}
     */
    static async prompter(): Promise<void> {
        await BranchOrigin.dealBranchMessage();

        // 判断是否是自动构建的分支，自动获取数据
        if (BranchOrigin.gitBranch.includes('‖')) {
            const spList = BranchOrigin.gitBranch.split('‖');

            this.JIRADATA = spList[1].includes('ˉ') ? spList[1].split('ˉ') : spList[1];

            this.COMMITDATA = {
                type: spList[0],
                body: await CommitBranchInquirer.getBodyInquirer()
            };
        } else {
            this.JIRADATA = await JiraInquirer.jiraInquirer();

            this.COMMITDATA = await CommitBranchInquirer.getCommitInquirer();
        }
    }

    /**
     * 处理提交信息
     */
    static dealCommitData(): string[] {
        const wrapOptions = {
            trim: true,
            cut: false,
            newline: '\n',
            indent: '',
            width: 100
        };

        const checkJiraType = typeof this.JIRADATA === 'string';

        // jira号
        const scope = '(' + (checkJiraType ? '' : this.JIRADATA[0]) + ')';

        // 请求头部
        const head = this.COMMITDATA.type + scope + ': ' + MessageDeal.removeSpace(checkJiraType ? this.JIRADATA as string : this.JIRADATA[1]);

        // 自动处理详细信息换行问题
        const body = this.COMMITDATA.body ? wrap(this.COMMITDATA.body, wrapOptions) : '';

        return [head, body];
    }
}
